package com.goxueche.lib_core;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Handler;
import android.util.Log;

import androidx.multidex.MultiDexApplication;

import com.bumptech.glide.Glide;
import com.facebook.stetho.Stetho;
import com.goxueche.lib_core.utils.NiceLogUtil;
import com.goxueche.lib_core.utils.file.FileUtil;

import java.io.Serializable;


/**
 * @author 哈兹米
 * @Description: 核心Application
 */
public class CoreApplication extends MultiDexApplication implements Serializable {

    public static final long serialVersionUID = 4656071326644680147L;
    public static Context context;
    public static boolean IS_EXIST_SDCARD;
    public static String CACHE_DIR_SD; // SD卡缓存目录
    public static String CACHE_DIR_SYSTEM; // 系统目录
    public static String IMAGE_DIR; // 图片目录
    public static String FILE_DIR; // 文件目录
    public static String LOG_DIR; // 日志目录
    public static String IMAGE_UPLOAD_TEMP; // 上传图片临时目录
    public static String LOG; // 日志保存的SD卡的目录
    public static String AllLOG;

    /**
     * 项目中用到的全局Handler
     */
    protected static Handler mHandler;
    // 关于图片缓存的处理方案以及涉及到的一些常量
    private static CoreApplication coreApp;

    /**
     * 取得单例的核心Applicatio 实例
     **/
    public static CoreApplication getInstance() {
        return coreApp;
    }

    public static Context getContext() {
        return context;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        coreApp = this;
        Stetho.initializeWithDefaults(this);
        onCreateHandler();
//        RxJavaPlugins.setErrorHandler(e -> {
//            if (e instanceof UndeliverableException) {
//                e = e.getCause();
//            }
//            if ((e instanceof IOException) || (e instanceof SocketException)) {
//                // fine, irrelevant network problem or API that throws on cancellation
//                return;
//            }
//            if (e instanceof InterruptedException) {
//                // fine, some blocking code was interrupted by a dispose call
//                return;
//            }
//            if ((e instanceof NullPointerException) || (e instanceof IllegalArgumentException)) {
//                // that's likely a bug in the application
//                Thread.currentThread().getUncaughtExceptionHandler()
//                        .handleException(Thread.currentThread(), e);
//                return;
//            }
//            if (e instanceof IllegalStateException) {
//                // that's a bug in RxJava or in a custom operator
//                Thread.currentThread().getUncaughtExceptionHandler()
//                        .handleException(Thread.currentThread(), e);
//                return;
//            }
//            Log.warning("Undeliverable exception received, not sure what to do", e);
//        });
    }


    /**
     * @Description: 初始化
     */
    public void initFileDir() {
        if (FileUtil.isExistSD()) {
            // SD存在
            CACHE_DIR_SD = FileUtil.getSDCacheDir(context);
            IS_EXIST_SDCARD = true;
        } else {
            // 不存在则使用系统目录
            CACHE_DIR_SD = context.getCacheDir().getPath();
        }
        CACHE_DIR_SD += "/";
        NiceLogUtil.e("----SD卡目录---->>>:" + CACHE_DIR_SD);
        LOG = CACHE_DIR_SD + "cache.log";
        AllLOG = CACHE_DIR_SD + "allcache.log";
        IMAGE_DIR = CACHE_DIR_SD + "image/";
        FILE_DIR = CACHE_DIR_SD + "file/";
        LOG_DIR = CACHE_DIR_SD + "log/";
        IMAGE_UPLOAD_TEMP = CACHE_DIR_SD + "imageUploadTemp/";
        CACHE_DIR_SYSTEM = context.getCacheDir().getPath() + "/file/";
        FileUtil.checkDir(CACHE_DIR_SD);
        FileUtil.checkDir(IMAGE_DIR);
        FileUtil.checkDir(FILE_DIR);
        FileUtil.checkDir(LOG_DIR);
        // FileUtil.checkDir(IMAGE_UPLOAD_TEMP);
        FileUtil.checkDir(CACHE_DIR_SYSTEM);
        // TerminalUtils.getInstance(getApplicationContext()).initTerminalID();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }


    /**
     * 程序终止的时候执行
     */
    @Override
    public void onTerminate() {
        Log.d("Application", "onTerminate");
        super.onTerminate();
    }


    /**
     * 低内存的时候执行
     */
    @Override
    public void onLowMemory() {
        Log.d("Application", "onLowMemory");
        super.onLowMemory();
        Glide.get(this).clearMemory();
    }


    /**
     * HOME键退出应用程序
     * 程序在内存清理的时候执行
     */
    @Override
    public void onTrimMemory(int level) {
        Log.d("Application", "onTrimMemory");
        super.onTrimMemory(level);
        if (level == TRIM_MEMORY_UI_HIDDEN) {
            Glide.get(this).clearMemory();
        }
        Glide.get(this).trimMemory(level);
    }

    /**
     * 子类可以根据需要扩展handler的实现
     */
    protected void onCreateHandler() {
        mHandler = new Handler() {
        };
    }

    public static Handler getHandler() {
        return mHandler;
    }
}
